using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing;

namespace BlogEngine.Core
{
	public class PhotoEffectParameters
	{
		private Dictionary<string, string> _values = new Dictionary<string, string>();

		/// <summary>
		/// Initializes a new instance of the <see cref="PhotoEffectParameters"/> class.
		/// </summary>
		public PhotoEffectParameters()
		{
		}

		private bool _IsChanged = false;
		/// <summary>
		/// Gets if this object's data has been changed.
		/// </summary>
		/// <returns>A value indicating if this object's data has been changed.</returns>
		public bool IsChanged
		{
			get { return _IsChanged; }
			set { _IsChanged = value; }
		}

		public Dictionary<string, string>.KeyCollection Names
		{
			get
			{
				return _values.Keys;
			}
		}

		/// <summary>
		/// Gets or sets the <see cref="System.String"/> at the specified index.
		/// </summary>
		/// <value></value>
		public string this[string index]
		{
			get
			{
				return _values[index];
			}
			set
			{
				_values[index] = value;
				_IsChanged = true;
			}
		}

		public void Set(string index, string value)
		{
			if (_values.ContainsKey(index))
				_values.Remove(index);
			_values.Add(index, value);
			_IsChanged = true;
		}

		public void Set(string index, int value)
		{
			Set(index, value.ToString());
		}

		public void Set(string index, double value)
		{
			Set(index, value.ToString());
		}
		
		public void Set(string index, Color value)
		{
			Set(index, value.ToString());
		}

		public void Clear()
		{
			_values.Clear();
			_IsChanged = true;
		}

		public void Clear(string index)
		{
			if (_values.ContainsKey(index))
				_values.Remove(index);
			_IsChanged = true;
		}

		public string Get(string index, string defaultvalue)
		{
			if (string.IsNullOrEmpty(_values[index]))
				return defaultvalue;

			return _values[index];
		}

		public int Get(string index, int defaultvalue)
		{
			if (string.IsNullOrEmpty(_values[index]))
				return defaultvalue;

			int value = defaultvalue;

			if (!Int32.TryParse(_values[index], out value))
				return defaultvalue;

			return value;
		}

		public double Get(string index, double defaultvalue)
		{
			if (string.IsNullOrEmpty(_values[index]))
				return defaultvalue;

			double value = defaultvalue;

			if (!double.TryParse(_values[index], out value))
				return defaultvalue;

			return value;
		}

		protected static Color HexToColor(string hex)
		{
			string h = hex.Trim().TrimStart('[').TrimEnd(']').TrimStart('{').TrimEnd('}').TrimStart('#');

			if (h.Length == 6)
			{
				return Color.FromArgb(
					Uri.FromHex(h[1]) + (Uri.FromHex(h[0]) * 16),
					Uri.FromHex(h[3]) + (Uri.FromHex(h[2]) * 16),
					Uri.FromHex(h[5]) + (Uri.FromHex(h[4]) * 16));
			}

			if (h.Length == 8)
			{
				return Color.FromArgb(
					Uri.FromHex(h[1]) + (Uri.FromHex(h[0]) * 16),
					Uri.FromHex(h[3]) + (Uri.FromHex(h[2]) * 16),
					Uri.FromHex(h[5]) + (Uri.FromHex(h[4]) * 16),
					Uri.FromHex(h[7]) + (Uri.FromHex(h[6]) * 16));
			}

			if (h.Length == 3)
			{
				return Color.FromArgb(
					Uri.FromHex(h[1]) * 16,
					Uri.FromHex(h[2]) * 16,
					Uri.FromHex(h[3]) * 16);
			}

			if (h.Length == 4)
			{
				return Color.FromArgb(
					Uri.FromHex(h[1]) * 16,
					Uri.FromHex(h[2]) * 16,
					Uri.FromHex(h[3]) * 16,
					Uri.FromHex(h[4]) * 16);
			}

			return Color.Empty;
		}

		public Color Get(string index, Color defaultvalue)
		{
			if (string.IsNullOrEmpty(_values[index]))
				return defaultvalue;

			string value = _values[index];

			if (value.ToLower() == "transparent")
				return Color.Transparent;

			if ((value[0] == '[') || (value[0] == '{') || (value[0] == '#'))
				return HexToColor(value); 
			
			return Color.FromName(value);
		}
	}
}
